package kingwing.dc.core.sevice;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.json.JSONObject;

import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.processor.PageProcessor;
import us.codecraft.webmagic.selector.Json;
import us.codecraft.webmagic.selector.JsonPathSelector;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import kingwing.dc.core.model.Infos;
import kingwing.dc.core.model.Rule;
import kingwing.dc.core.model.RuleDetail;

/**
 * 列表+详细页面采集处理类（动态页面处理）
 * @author Kevin
 * 2017年10月27日
 */
public class JSProcessor implements PageProcessor {

    /**
     * 站点配置类
     */
    private Site site;
    
    /**
     * 采集规则明细-采集字段
     */
    private List<RuleDetail> ruleDetails;
    
    /*
     * 采集规则
     */
    private Rule rule;
    
	public JSProcessor(Site site, List<RuleDetail> ruleDetails, Rule rule) {
		super();
		this.site = site;
		this.ruleDetails = ruleDetails;
		this.rule = rule;
	}

	@Override
    public void process(Page page) {
        if (page.getUrl().regex(rule.getHelperUrlRegexes()).match()) {
        	//System.out.println(page.getRawText());
        	
        	String json= page.getRawText();
        	if (!json.startsWith("{") && !json.endsWith("}")) {
    			String reg = "\\{.+\\}";
    			Pattern pattern = Pattern.compile (reg);
    			Matcher matcher = pattern.matcher (json);
    			while (matcher.find ()) {
//    				System.out.println(matcher.group ());
    				json = matcher.group ();
    			}
    		}
        	
            List<String> ids = new JsonPathSelector(rule.getContentId()).selectList(json);
            if (CollectionUtils.isNotEmpty(ids)) {
                for (String id : ids) {
                	String ContentUrls = rule.getContentUrls();
                	if (StringUtils.isNotBlank(ContentUrls)) {
                		page.addTargetRequest(rule.getContentUrls() + id);
					}else {
						page.addTargetRequest(id);
					}
                }
            }
        } 
        if(page.getUrl().regex(rule.getContentUrlRegexes()).match()){
			Infos infor = new Infos();
			String flag = "用处：判断采集字段是否为空；空则跳过存储，反之存储结果";
			infor.setRuleName(rule.getRuleName());
			try {
				json:	for (RuleDetail ruleDetail : ruleDetails) {
					int i = ruleDetail.getFieldType();
					switch (i) {
					case 0:
						String field1 = new JsonPathSelector(ruleDetail.getRuleDetailSelector()).select(page.getRawText());
						if (StringUtils.isBlank(field1)) {
//							page.setSkip(true);
							flag=field1;
							break json;
						}
						infor.setField1(field1);
						break;
					case 1:
						String field2 = new JsonPathSelector(ruleDetail.getRuleDetailSelector()).select(page.getRawText());
						if (StringUtils.isBlank(field2)) {
//							page.setSkip(true);
							flag=field2;
							break json;
						}
						infor.setField2(field2);
						break;
					case 2:
						String field3 = new JsonPathSelector(ruleDetail.getRuleDetailSelector()).select(page.getRawText());
						if (StringUtils.isBlank(field3)) {
//							page.setSkip(true);
							flag=field3;
							break json;
						}
						field3 = formatToDate(field3);
						infor.setField3(field3);
						break;
					case 3:
						String field4 = new JsonPathSelector(ruleDetail.getRuleDetailSelector()).select(page.getRawText());
						if (StringUtils.isBlank(field4)) {
//							page.setSkip(true);
							flag=field4;
							break json;
						}
						infor.setField4(field4);
						break;
					case 4:
						String field5 = new JsonPathSelector(ruleDetail.getRuleDetailSelector()).select(page.getRawText());
						if (StringUtils.isBlank(field5)) {
//							page.setSkip(true);
							flag=field5;
							break json;
						}
						infor.setField5(field5);
						break;
					case 5:
						String field6 = new JsonPathSelector(ruleDetail.getRuleDetailSelector()).select(page.getRawText());
						if (StringUtils.isBlank(field6)) {
//							page.setSkip(true);
							flag=field6;
							break json;
						}
						infor.setField6(field6);
						break;
					case 6:
						String field7 = new JsonPathSelector(ruleDetail.getRuleDetailSelector()).select(page.getRawText());
						infor.setField7(new JsonPathSelector(ruleDetail.getRuleDetailSelector()).select(page.getRawText()));
						if (StringUtils.isBlank(field7)) {
//							page.setSkip(true);
							flag=field7;
							break json;
						}
						break;
					case 7:
						String field8 = new JsonPathSelector(ruleDetail.getRuleDetailSelector()).select(page.getRawText());
						infor.setField8(new JsonPathSelector(ruleDetail.getRuleDetailSelector()).select(page.getRawText()));
						if (StringUtils.isBlank(field8)) {
//							page.setSkip(true);
							flag=field8;
							break json;
						}
						infor.setField8(field8);
						break;
						
					default:
						System.out.println("字段用完了！");
						break;
					}
				}
				
			} catch (Exception e) {
				// TODO: handle exception
				rule:for (RuleDetail ruleDetail : ruleDetails) {
					int i = ruleDetail.getFieldType();
					switch (i) {
					case 0:
						String field1 = page.getHtml().xpath(ruleDetail.getRuleDetailSelector()).toString();
						if (StringUtils.isBlank(field1)) {
							page.setSkip(true);
							flag=field1;
							break rule;
						}
						infor.setField1(field1);
						break;
					case 1:
						String field2 = page.getHtml().xpath(ruleDetail.getRuleDetailSelector()).toString();
						if (StringUtils.isBlank(field2)) {
							page.setSkip(true);
							flag=field2;
							break rule;
						}
						infor.setField2(field2);
						break;
					case 2:
						String field3 = page.getHtml().xpath(ruleDetail.getRuleDetailSelector()).toString();
						if (StringUtils.isBlank(field3)) {
							page.setSkip(true);
							flag=field3;
							break rule;
						}
						field3 = formatToDate(field3);
						infor.setField3(field3);
						break;
					case 3:
						String field4 = page.getHtml().xpath(ruleDetail.getRuleDetailSelector()).toString();
						if (StringUtils.isBlank(field4)) {
							page.setSkip(true);
							flag=field4;
							break rule;
						}
						infor.setField4(field4);
						break;
					case 4:
						String field5 = page.getHtml().xpath(ruleDetail.getRuleDetailSelector()).toString();
						if (StringUtils.isBlank(field5)) {
							page.setSkip(true);
							flag=field5;
							break rule;
						}
						infor.setField5(field5);
						break;
					case 5:
						String field6 = page.getHtml().xpath(ruleDetail.getRuleDetailSelector()).toString();
						if (StringUtils.isBlank(field6)) {
							page.setSkip(true);
							flag=field6;
							break rule;
						}
						infor.setField6(field6);
						break;
					case 6:
						String field7 = page.getHtml().xpath(ruleDetail.getRuleDetailSelector()).toString();
						if (StringUtils.isBlank(field7)) {
							page.setSkip(true);
							flag=field7;
							break rule;
						}
						infor.setField7(field7);
						break;
					case 7:
						String field8 = page.getHtml().xpath(ruleDetail.getRuleDetailSelector()).toString();
						if (StringUtils.isBlank(field8)) {
							page.setSkip(true);
							flag=field8;
							break rule;
						}
						infor.setField8(field8);
						break;

					default:
						System.out.println("字段用完了！");
						break;
					}
				}
			}
			if (StringUtils.isNotBlank(flag)) {
				page.putField("infos", infor);
			}
		}

    }

	/**
	 * 返回一个格式良好的日期字符串
	 * @param field3
	 * @return
	 */
	private String formatToDate(String field3) {
		// TODO Auto-generated method stub
		String reg = "([0-9]{4})[-/\\.年]?([0-1]?[0-9]{1})[-/\\.月]?([0-3]?[0-9]{1})[日]?";//日期正则表达式
		Pattern pattern = Pattern.compile (reg);
		Matcher matcher = pattern.matcher (field3);//使用正则表达式判断日期
		if (matcher.find ()) {
			return matcher.group ();
		}else {
			return field3;
		}
	}
	
    @Override
    public Site getSite() {
        return site;
    }
}